package com.medusa.aps.business.modules.basic.controller;


import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.basic.model.input.CustomerInformationDTO;
import com.medusa.aps.business.modules.basic.model.input.CustomerInformationParam;
import com.medusa.aps.business.modules.basic.mp.entity.CustomerInformation;
import com.medusa.aps.business.modules.basic.mp.service.CustomerInformationService;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 客户信息控制层
 *
 * @author yuxh
 * @since 2023-08-14 17:37:43
 */
@Validated
@RestController
@RequestMapping("/customerInformation")
@Tag(name = "客户信息控制层", description = "客户信息控制层")
public class CustomerInformationController {
    /**
     * 服务对象
     */
    @Resource
    private CustomerInformationService customerInformationService;

    /**
     * 分页查询客户信息
     *
     * @param customerInformationParam 分页对象
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    @Operation(summary = "分页查询客户信息")
    public Result<Page<CustomerInformation>> pageCustomerInformation(CustomerInformationParam customerInformationParam) {
        return Result.ok(
                customerInformationService.pageCustomerInformation(customerInformationParam)
        );
    }

    /**
     * 通过主键查询客户信息
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    @Operation(summary = "通过主键查询客户信息")
    public Result<CustomerInformation> selectOne(@RequestParam("id") Integer id) {
        return Result.ok(customerInformationService.getById(id));
    }

    /**
     * 批量新增客户
     *
     * @param customerInformationList 客户信息集合
     */
    @PostMapping("/save")
    @Operation(summary = "批量新增客户")
    public Result<Void> saveBatchCustomerInformation(@RequestBody @Valid @Size(min = 1, max = 50, message = "最多添加50个") List<CustomerInformationDTO> customerInformationList) {
        customerInformationService.saveBatchCustomerInformation(customerInformationList);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param customerInformationDTO 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改数据")
    public Result<Void> update(@RequestBody @Validated(Update.class) CustomerInformationDTO customerInformationDTO) {
        customerInformationService.updateCustomerInformation(customerInformationDTO);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param ids 主键ids
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除数据")
    public Result<Boolean> delete(@RequestBody Set<Long> ids) {
        customerInformationService.delete(ids);
        return Result.ok();
    }

    /**
     * 根据客户编码或客户名称查询客户基本信息
     *
     * @return 客户基本信息集合
     */
    @GetMapping("/getCustomerInformation")
    @Operation(summary = "根据客户编码或客户名称查询客户基本信息")
    public Result<List<CustomerInformation>> getCustomerInformation(@RequestParam(required = false) String customerCode,
                                                                    @RequestParam(required = false) String customerName,
                                                                    @RequestParam(required = false) String customerCompanyName) {
        return Result.ok(customerInformationService.getCustomerInformation(customerCode, customerName, customerCompanyName));
    }
}

